<?php
	/** 注册功能类 
	@author ffpan
	@date 2010-04-06
	@version 1.0
	**/

	class House{
		function __construct(){
			$this->fangDb = new wbDatabase();
			$this->order = array(
				1 => "order by postdate desc", //更新日期排名
				2 => "order by price desc", //价格由高到低
				3 => "order by price asc", //价格由低到高
				4 => "order by square desc", //面积由大到小
				5 => "order by square asc", //面积由小到大
				6 => "order by plaza asc" //面积由小到大
			);
		}

		function __destruct(){}

		function __get($att){
			return $att;
		}

		function __set($att , $val){
			$this->$att = $val;
		}

		function safeVal($val){
			if (!get_magic_quotes_gpc()) {
				 $val = addslashes($val);
			}
			return $val;
		}

		function safePostVal(){
			if (!get_magic_quotes_gpc()) {
			   foreach($_POST as &$v){
				 $v = addslashes($v);
			   }
			}
			return $val;
		}
		
	
		
		//选出出售房源
		function getSellList($type){
			$sql = "select * from ".__table_sellhouse__." where ispublic = 1 ".$this->order[$type]." limit ".__index_shouse_num__;
			$arr = array();
			if ($result = $this->fangDb->query($sql)) {
			   while ($row = $result->fetch_assoc()) {
				    $arr[] = $row;
			   }
			   $result->close();
			}

			foreach($arr as &$value){
				$value["publisher"] = $this->getPublisher($value["prop"] , $value["aid"] , $value["uid"]);
				$value["postdate"] = $this->getshowtime($value["postdate"]);
			}
			return $arr;
		}

		function liveCondtion($condition){
			if($condition==""){return 1;}
			$arr = array();
			foreach($condition as $key=>$value){
				$value  = $this->safeVal($value);
				if($value==""){continue;}
				if($key=="aid"){$arr[] = $key."=".$value." ";continue;}
				if(($key=="style"||$key=="floor")&&!(strpos($value, "+")==true)){$arr[] = $key." like '".$value."%' ";continue;}
				if(strpos($value, "+")==true){
					$arr[] = $key.">=".intval($value)." ";
				}else if(strpos($value , "-")==true){
					$t = explode("-" , $value);
					$arr[] = $key.">=".intval($t[0])." and ".$key."<".intval($t[1])." ";
				}else if(strpos($value , "~")==true){
					$t = str_replace("~" , "" , $value);
					$arr[] = $key."<>".$t." ";
				}else{
					$arr[] = $key." like '%".$value."%' ";
				}
			}
			return implode(" and " , $arr);
		}

		function plazaName($pid){
			$pid = $this->safeVal($pid);
			$sql = "select name from ".__table_plaza__." where id='".$pid."' limit 1";
			$result = $this->fangDb->query($sql);
			if($result){
				$row = $result->fetch_array();
				return $row[0];
			}else{
				return "";
			}
		}

		function getSearchPlaza(){
			$plaza_arr = array();
			$catArr = array();
			foreach($this->listArr as $row){
				if(!in_array($row["plaza"] , $plaza_arr)){
					$plaza_arr[]=$row["plaza"];
					$sql = "select id from ".__table_plaza__." where name='".$row["plaza"]."'";
					$result = $this->fangDb->query($sql);
					if($result){
						$temp = $result->fetch_array();
						if($temp[0]!=""){$catArr[]=$temp[0];}
						$result->close();
					}
				}
			}
			return implode("|" , $catArr);
		}
		
		function getTable($sorr){
			if($sorr=="s"){
				return __table_sellhouse__;
			}else if($sorr=="r"){
				return __table_renthouse__;
			}
		}
		
		function dealListData(&$arr){
			foreach($arr as &$value){
				$value["publisher"] = $this->getPublisher($value["prop"] , $value["aid"] , $value["uid"]);
				$value["postdate"] = $this->getshowtime($value["postdate"]);
				$value["style_detail"] = $this->getStyleDetail($value["style"] , $value["mstyle"]);
				$value["floor_detail"] = $this->getFloorDetail($value["floor"]);
			}
			
			return $arr;
		}
		
		//获取首页最新的出租出售信息
		function getIndexNewList($sorr , $condition , $type , $p=-1 , $limit){
			$where = $this->liveCondtion($condition);
			$limit = " limit 0 , ".$limit;
			$table = $this->getTable($sorr);

			$sql = "select * from ".$table." where ".$where." ".$this->order[$type].$limit;
			$arr = array();
			
			if ($result = $this->fangDb->query($sql)){
			   while ($row = $result->fetch_assoc()) {
				    $arr[] = $row;
			   }
			   $result->close();
			}

			$this->listArr = $this->dealListData($arr); 

			return $arr;
			
		}
		
		function getSearchSellList($condition , $type , $p=-1 , $pernum=__search_shouse_num__){
			$where = $this->liveCondtion($condition);
			
			if($p!=-1){
				$pt = $this->getSearchPage($condition,$pernum);
				if($p<1){$p=1;}
				if($p>$pt+1){$p = $pt+1;}

				$limit = " limit ".($p-1)*$pernum." , ".$pernum;
			}

			$sql = "select * from ".__table_sellhouse__." where ".$where." ".$this->order[$type].$limit;
			$arr = array();
			
			if ($result = $this->fangDb->query($sql)){
			   while ($row = $result->fetch_assoc()) {
				    $arr[] = $row;
			   }
			   $result->close();
			}

			foreach($arr as &$value){
				$value["publisher"] = $this->getPublisher($value["prop"] , $value["aid"] , $value["uid"]);
				$value["postdate"] = $this->getshowtime($value["postdate"]);
				$value["updatedate"] = $this->getshowtime($value["updatedate"]);
				$value["style_detail"] = $this->getStyleDetail($value["style"] , $value["mstyle"]);
				$value["floor_detail"] = $this->getFloorDetail($value["floor"]);
			}

			$this->listArr = $arr; 

			return $arr;
		}
		
		function getStyleDetail($s , $ms){
			$str = "";
			$sstr = "";
			$sarr = explode("/" , $s);
			$str = $sarr[0]."室". $sarr[1]."厅". $sarr[2]."卫";
			$msarr = explode("|" , $ms);
			if($msarr[0]=="ck"){$sstr="车";}
			if($msarr[1]=="gl"){$sstr="阁";}
			if($sstr!=""){
				$str .= "(".$sstr.")";
			}
			return $str;
		}
		
		function getFloorDetail($f){
			$arr = explode("/" , $f);
			return $arr[0]."楼(".$arr[1].")";
		}
		
		function getSearchNum($condition){
			$where = $this->liveCondtion($condition);
			$sql = "select count(*) from ".__table_sellhouse__." where ".$where;
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_array();
			return $row[0];
		}
		
		function getSearchNum2($condition){
			$where = $this->liveCondtion($condition);
			$sql = "select count(*) from ".__table_renthouse__." where ".$where;
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_array();
			return $row[0];
		}

		function getSearchPage($condition , $pernum){
			$temp = $this->getSearchNum($condition);
			$page =  ceil($temp/$pernum);
			return $page;
		}

		function getSearchPage2($condition , $pernum){
			$temp = $this->getSearchNum2($condition);
			$page =  ceil($temp/$pernum);
			return $page;
		}

		function getSearchRentList($condition , $type , $p=-1 , $pernum=__search_rhouse_num__){
			$where = $this->liveCondtion($condition);
			
			if($p!=-1){
				$pt = $this->getSearchPage2($condition , $pernum);
				if($p<1){$p=1;}
				if($p>$pt+1){$p = $pt+1;}

				$limit = " limit ".($p-1)*$pernum." , ".$pernum;
			}

			$sql = "select * from ".__table_renthouse__." where ".$where." ".$this->order[$type].$limit;

			$arr = array();
			if ($result = $this->fangDb->query($sql)) {
			   while ($row = $result->fetch_assoc()) {
				    $arr[] = $row;
			   }
			   $result->close();
			}

			foreach($arr as &$value){
				$value["publisher"] = $this->getPublisher($value["prop"] , $value["aid"] , $value["uid"]);
				$value["postdate"] = $this->getshowtime($value["postdate"]);
				$value["updatedate"] = $this->getshowtime($value["updatedate"]);
				$value["style_detail"] = $this->getStyleDetail($value["style"] , $value["mstyle"]);
				$value["floor_detail"] = $this->getFloorDetail($value["floor"]);
			}

			$this->listArr = $arr; 

			return $arr;
		}

		//选出出售房源
		function getRentList($type){
			$sql = "select id , aid , uid , prop , title , square , floor , price , isfull ,  postdate from ".__table_renthouse__." where ispublic = 1 ".$this->order[$type]." limit ".__index_rhouse_num__;
			$arr = array();
			if ($result = $this->fangDb->query($sql)) {
			   while ($row = $result->fetch_assoc()) {
				    $arr[] = $row;
			   }
			   $result->close();
			}
			


			foreach($arr as &$value){
				$value["publisher"] = $this->getPublisher($value["prop"] , $value["aid"] , $value["uid"]);
				$value["postdate"] = $this->getshowtime($value["postdate"]);
				$value["updatedate"] = $this->getshowtime($value["updatedate"]);
			}
			return $arr;
		}

		
		//获取出售房源详细信息
		function getShouseInfo($id){
			$id = $this->safeVal($id);
			$sql = "select * from ".__table_sellhouse__." where id = '".$id."' limit 1";
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_assoc();
			$row["publisher"] = $this->getPublisher($row["prop"] , $row["aid"] , $row["uid"]);
			$row["postdate"] = $this->getshowtime($row["postdate"]);
			$value["updatedate"] = $this->getshowtime($value["updatedate"]);
			$c = $this->getContaction($row["prop"] , $row["aid"] , $row["uid"]);
			$row["mobile"] = $c["mobile"];
			$row["xlt"] = $c["xlt"];
			$row["tele"] = $c["tele"];
			$row["perprice"] = round($row["price"]/$row["square"] , 2);
			$row["style_detail"] = $this->getStyleDetail($row["style"] , $row["mstyle"]);
			$row["floor_detail"] = $this->getFloorDetail($row["floor"]);
			
			return $row;
		}

		//获取出租房源详细信息
		function getRhouseInfo($id){
			$id = $this->safeVal($id);
			$sql = "select * from ".__table_renthouse__." where id = '".$id."' limit 1";
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_assoc();
			$row["publisher"] = $this->getPublisher($row["prop"] , $row["aid"] , $row["uid"]);
			$row["postdate"] = $this->getshowtime($row["postdate"]);
			$value["updatedate"] = $this->getshowtime($value["updatedate"]);
			$c = $this->getContaction($row["prop"] , $row["aid"] , $row["uid"]);
			$row["mobile"] = $c["mobile"];
			$row["xlt"] = $c["xlt"];
			$row["tele"] = $c["tele"];
			$row["isfull"] = $row["isfull"]==1?"整租":"合租";
			$row["style_detail"] = $this->getStyleDetail($row["style"] , $row["mstyle"]);
			$row["floor_detail"] = $this->getFloorDetail($row["floor"]);
			return $row;
		}



		function getStyle($s){
			$arr = explode('/' , $s);
			return $arr[0]."室".$arr[1]."厅".$arr[2]."卫";
		}

		function getPublisher($prop , $aid , $uid){
			if($prop==0){
				$sql="select `name` from ".__table_agentlist__." where id='".$aid."' limit 1";
			}else{
				$sql="select `name` from ".__table_userlist__." where id='".$uid."' limit 1";
			}
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_array();
			$publisher = $row[0];
			$result->close();
			return $publisher;
		}
		
		function getContaction($prop , $aid , $uid){
			if($prop==0){
				$sql="select mobile, xlt , tele from ".__table_agentlist__." where id='".$aid."' limit 1";
			}else{
				$sql="select mobile from ".__table_userlist__." where id='".$uid."' limit 1";
			}
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_assoc();
			$result->close();
			return $row;
		}

		function getPublisherIdByHid($hid,$type){
			if(!isset($hid)){return false;}
			$hid = $this->safeVal($hid);
			$table = $type==0?__table_sellhouse__:__table_renthouse__;
			$sql = "select aid , uid from ".$table." where id=".$hid;
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_array();
			$aid = $row[0];
			$uid = $row[1];
			$id = $aid==-1?$uid:$aid;
			$result->close();
			return $id;
		}

		function getshowtime($time){
			$t = explode(" " , $time);
			$t0 = explode("-" , $t[0]);
			$t1= explode(":" , $t[1]);

			return ($t0[1]."-".$t0[2]." ".$t1[0].":".$t1[1]);
		}
		
		//设置最近浏览
		function setCollection($id , $plaza , $style){
			$str =implode("&nbsp;" ,  array($plaza,$style));
			$time = time()+60*60*24*7;
			setcookie("fsw_recent[".$id."]", $str , $time);  
			
			
			//setcookie("cookie[three]","cookiethree");
			//setcookie("cookie[two]","cookietwo");
			//setcookie("cookie[one]","cookieone");
			
			
		}
		
		function getCollection(){
			$arr = array();
			$i = 0;
			if (isset($_COOKIE["fsw_recent"])){
				$carr = array_reverse($_COOKIE["fsw_recent"]);
				foreach ($carr as $name => $value){
					$i++;
					if($i>8){
						setcookie("fsw_recent[".$name."]", "" , time()-1 );  
						continue;
					}
					$temp = explode("_" , $name);
					$type = $temp[0];
					$id = $temp[1];
					$arr[] = array(
						"link" => $type=="s"?"sellhouse_detail.php?sid=".$id : "renthouse_detail.php?rid=".$id,
						"title" => $type=="s"?"[出售]".$value : "[出租]".$value
					);
					
				}
			}
			return $arr;
		}

	}

	$myHouse = new House();
?>